<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>自动加载对象</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.constants.html">类常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.decon.html">构造函数和析构函数</a></div>
 <div class="up"><a href="language.oop5.html">类与对象</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.oop5.autoload" class="sect1">
 <h2 class="title">自动加载对象</h2>
 <p class="para">
  很多开发者写面向对象的应用程序时对每个类的定义建立一个
  PHP 源文件。一个很大的烦恼是不得不在每个脚本开头写一个长长的包含文件列表（每个类一个文件）。
 </p>
 <p class="para">
  在 PHP 5 中，不再需要这样了。可以定义一个
  <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> 函数，它会在试图使用尚未被定义的类时自动调用。通过调用此函数，脚本引擎在
  PHP 出错失败前有了最后一个机会加载所需的类。
 </p>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    <span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span> 提供了一种更加灵活的方式来实现类的自动加载。因此，不再建议使用 <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> 函数，在以后的版本中它可能被弃用。
   </p>
  </div>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
    在 5.3.0 版之前，__autoload 函数抛出的异常不能被 <a href="language.exceptions.html" class="link">catch</a>
    语句块捕获并会导致一个致命错误。从 5.3.0+ 之后，__autoload 函数抛出的异常可以被
    <a href="language.exceptions.html" class="link">catch</a>
    语句块捕获，但需要遵循一个条件。如果抛出的是一个自定义异常，那么必须存在相应的自定义异常类。__autoload
    函数可以递归的自动加载自定义异常类。
  </p>
 </p></blockquote>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   自动加载不可用于 PHP 的 CLI <a href="features.commandline.html" class="link">交互模式</a>。
  </p>
 </p></blockquote>
 <p class="para">
  <div class="example" id="example-177">
   <p><strong>Example #1 自动加载示例</strong></p>
   <div class="example-contents"><p>
    本例尝试分别从 <var class="filename">MyClass1.php</var>
    和 <var class="filename">MyClass2.php</var> 文件中加载
    <em>MyClass1</em> 和
    <em>MyClass2</em> 类。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$class_name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;require_once&nbsp;</span><span style="color: #0000BB">$class_name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$obj&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass1</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$obj2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass2</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-178">
   <p><strong>Example #2 另一个例子</strong></p>
   <div class="example-contents"><p>
    本例尝试加载接口 <em>ITest</em>。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">Foo&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">ITest&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">/*<br />string(5)&nbsp;"ITest"<br /><br />Fatal&nbsp;error:&nbsp;Interface&nbsp;'ITest'&nbsp;not&nbsp;found&nbsp;in&nbsp;...<br />*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-179">
   <p><strong>Example #3 自动加载在 PHP 5.3.0+ 中的异常处理</strong></p>
   <div class="example-contents"><p>
    本例抛出一个异常并在 try/catch 语句块中演示。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Want&nbsp;to&nbsp;load&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;load&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">."</span><span style="color: #007700">);<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">NonLoadableClass</span><span style="color: #007700">();<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Want to load NonLoadableClass.
Unable to load NonLoadableClass.
</pre></div>
   </div>
  </div>
  <div class="example" id="example-180">
   <p><strong>Example #4 自动加载在 PHP 5.3.0+ 中的异常处理 - 没有自定义异常机制</strong></p>
   <div class="example-contents"><p>
    本例将一个异常抛给不存在的自定义异常处理函数。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Want&nbsp;to&nbsp;load&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">MissingException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;load&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">."</span><span style="color: #007700">);<br />}<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">NonLoadableClass</span><span style="color: #007700">();<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Want to load NonLoadableClass.
Want to load MissingException.

Fatal error: Class &#039;MissingException&#039; not found in testMissingException.php on line 4
</pre></div>
   </div>
  </div>
 </p>

 <div class="simplesect">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.unserialize.html" class="function">unserialize()</a></span></li>
    <li class="member"><a href="var.configuration.html#unserialize-callback-func" class="link">unserialize_callback_func</a></li>
    <li class="member"><span class="function"><a href="function.spl-autoload.html" class="function">spl_autoload()</a></span></li>
    <li class="member"><span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.constants.html">类常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.decon.html">构造函数和析构函数</a></div>
 <div class="up"><a href="language.oop5.html">类与对象</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
